<?php
/**
 * Created by PhpStorm.
 * User: LG
 * Date: 2017/8/30
 * Time: 14:12
 */

namespace app\common\model;

use think\Model;
use think\Db;

/**
 *
 * 总后台权限操作
 *
 */
class AdminRole extends Model
{
    protected $db;

    public function __construct()
    {
        parent::__construct();
        $this->db = Db::name("admin_auth_group");
    }

    /**
     *  角色分页显示
     * @author  李港
     * @param   int     $page   当前页
     * @param   array   $where  条件（一维数组）
     * @return  array
     */
    public function selectPage($page = 1,$where=array())
    {
        $Configuration = new Configuration();
        $limits = intval($Configuration->getkey('pagesize'));
        $count = $this->db->where($where)->count();//计算总页面
        $allpage = intval(ceil($count / $limits));
        $lists = $this->db->where($where)->page($page, $limits)->select();
        $data = array();
        $data['data'] = $lists;
        $data['page'] = [
            'count'=>$count,
            'allpage'=>$allpage,
        ];
        return $data;
    }

    /**
     *  单独查询一条数据
     * @author  李港
     * @param   array   $where  条件（一维数组）
     * @return  array
     */
    public function findData($where=array())
    {
        return $this->db->where($where)->find();
    }

    /**
     *  添加一条数据
     * @author  李港
     * @param   array   $data  条件（一维数组）
     * @return  int
     */
    public function insertData($data)
    {
        $result = $this->db->insert($data);
        if($result){
            writelog($this->user_data['uid'],$this->user_data['username'],'用户【'.$this->user_data['username'].'】添加成功：角色名称',0,$this->db_name_log);
        }
        writelog($this->user_data['uid'],$this->user_data['username'],'用户【'.$this->user_data['username'].'】添加失败：角色名称：',0,$this->db_name_log);
        return $result;
    }

    /**
     *  更新一条数据
     * @author  李港
     * @param   array   $data  条件（一维数组）
     * @return  int
     */
    public function updateData($data)
    {
        $result = $this->db->update($data);
        if($result){
            writelog($this->user_data['uid'],$this->user_data['username'],'用户【'.$this->user_data['username'].'】更新成功：角色名称',0,$this->db_name_log);
        }
        writelog($this->user_data['uid'],$this->user_data['username'],'用户【'.$this->user_data['username'].'】更新失败：角色名称：',0,$this->db_name_log);
        return $result;
    }
}